package com.weibo.zhuanfa.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

import com.weibo.common.BaseSeverlet;
import com.weibo.zhuanfa.ZhuanFa;

public class ZhuanfaServlet extends BaseSeverlet {

	private static Logger logger = Logger.getLogger(ZhuanfaServlet.class);

	@Override
	public void doService(HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("直发ZhuanfaServlet被请求了！");
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
		ServletFileUpload servletFileUpload = new ServletFileUpload(
				diskFileItemFactory);

		ZhuanFa zhuanfa = new ZhuanFa();
		String starttime = "";
		String hour = "";
		String minute = "";
		String celue = "";
		HttpSession session = request.getSession();
		try {
			List<FileItem> fileItems = servletFileUpload.parseRequest(request);
			for (FileItem fileItem : fileItems) {
				if (fileItem.isFormField()) {
					String requestOfValue = fileItem.getString("UTF-8");
					String requestForName = fileItem.getFieldName();

					if (requestForName.equals("pingtai")) {
						zhuanfa.setPla_id(requestOfValue);
					}
					if (requestForName.equals("task_type")) {
						zhuanfa.setTas_type(requestOfValue);
					}
					if (requestForName.equals("content_type")) {
						zhuanfa.setTas_cont_type(requestOfValue);
					}
					if (requestForName.equals("taskname")) {
						zhuanfa.setTas_name(requestOfValue);
					}
					if (requestForName.equals("zhuanfayu")) {
						zhuanfa.setZhuanf_content(requestOfValue);
					}
					if (requestForName.equals("otherContent")) {
						zhuanfa.setTas_other(requestOfValue);
					}
					if (requestForName.equals("forwardlink")) {
						zhuanfa.setZhuanf_href(requestOfValue);
					}
					if (requestForName.equals("starttime")) {
						starttime = requestOfValue;
					}
					if (requestForName.equals("hour")) {
						hour = requestOfValue;
					}
					if (requestForName.equals("minute")) {
						minute = requestOfValue;
					}
					if (requestForName.equals("celue")) {
						celue = requestOfValue;
					}
				} else {
					// // 获得上传文件的名字 (文件上传框中, 上传文件的名字, 注意: 该名字已被截取, 并不是文件的完整路径)
					// String fileName = fileItem.getName();
					// if (fileName.contains("\\")) {
					// fileName = fileName.substring(
					// fileName.lastIndexOf("\\") + 1,
					// fileName.length());
					// }
					// // 用户选择了上传的文件, 并且该文件的名字不为空
					// if (fileItem.getName() != null
					// && !"".equals(fileItem.getName())) {
					// // 获得上传文件的类型
					// String contentType = fileItem.getContentType();
					//
					// // 获得上传文件的大小
					// long fileSize = fileItem.getSize();
					//
					// // 打印Http请求消息 ('Request') 中上传文件的信息
					// System.out.println("上传的文件名: " + fileName);
					// System.out.println("上传的文件类型: " + contentType);
					// System.out.println("上传的文件大小: " + fileSize);
					//
					// // 获得项目的根目录
					// String rootDirProject = getServletContext()
					// .getRealPath("/");
					//
					// // 拼接保存的上传文件的目录
					// File saveFileUploadFile = new File(rootDirProject
					// + "zhuanfaupload");
					//
					// // 如果保存上传文件的目录不存在, 创建
					// if (!saveFileUploadFile.exists()) {
					// saveFileUploadFile.mkdirs();
					// }
					//
					// // 拼接保存上传内容的文件
					// File saveTheFileUpload = new File(saveFileUploadFile,
					// fileName);
					// // 保存上传的文件
					// fileItem.write(saveTheFileUpload);
					// if (contentType.indexOf("image") >= 0) {
					// zhuanfa.setZhif_img(request.getScheme() + "://"
					// + request.getServerName() + ":"
					// + request.getServerPort()
					// + getServletContext().getContextPath()
					// + "/upload/" + fileName);
					// }
					// }
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		zhuanfa.setTas_start_time(starttime + " " + hour + ":" + minute);
		session.setAttribute("zhuanfa", zhuanfa);
		request.setAttribute("celue", celue);
		try {
			request.getRequestDispatcher("/Task_add_fordard_confirm.jsp")
					.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
